package com.jhws.business.comm.dao;

import java.math.BigInteger;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

import com.jhws.business.comm.bo.TblBuildingDimBo;
import com.jhws.common.Page;
import com.jhws.core.db.BaseDao;

@Transactional
public class TblBuildingDimHibernateDAO extends BaseDao implements ITblBuildingDimDAO {

    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public TblBuildingDimBo getById(String BuildingCode) {
        @SuppressWarnings("unchecked")
        Query q = sessionFactory.getCurrentSession().createQuery("from TblBuildingDimBo where building_code = ? ");
        q.setParameter(0, BuildingCode);
        List<TblBuildingDimBo> lists = q.list();
        if (lists.size() != 0)
            return (TblBuildingDimBo) lists.get(0);
        else
            return null;
    }

    public TblBuildingDimBo getBuildById(String BuildingCode) {

        Query q = sessionFactory.getCurrentSession().createQuery("from TblBuildingDimBo where building_code = ? ");
        q.setParameter(0, BuildingCode);
        List list = q.list();
        if (list.size() != 0)
            return (TblBuildingDimBo) list.get(0);
        else
            return null;
    }

    public Page findTblBuildingDim(int start, int num, String whereClause)
            throws Exception {
        try {
            StringBuffer buf = new StringBuffer();
            buf.append("from TblBuildingDimBo ");
            buf.append(whereClause);

            Query query = sessionFactory.getCurrentSession().createQuery(buf.toString());
            query.setFirstResult(start);
            query.setMaxResults(num);

            @SuppressWarnings("unchecked")
            List<TblBuildingDimBo> list = query.list();

            Page page = new Page(start, num, list);
            String hql = "select count(*) from TblBuildingDimBo  " + (whereClause == null || "".equals(whereClause.trim()) ? "" : whereClause.trim());
            query = sessionFactory.getCurrentSession().createQuery(hql);

            page.setTotalNumber(((Number) query.uniqueResult()).intValue());
            return page;
        } catch (Exception e) {
            System.out.println("=========e:" + e.getMessage());
            return new Page();
        }
    }

    public TblBuildingDimBo save(TblBuildingDimBo bo) {
        sessionFactory.getCurrentSession().saveOrUpdate(bo);
        return bo;
    }

    public List<TblBuildingDimBo> findTblBuildingDimByWhere(String whereClause) throws Exception {
        StringBuilder buf = new StringBuilder("from TblBuildingDimBo ");
        if (!StringUtils.isEmpty(whereClause)) {
            buf.append(whereClause);
        }
        Query query = sessionFactory.getCurrentSession().createQuery(buf.toString());

        @SuppressWarnings("unchecked")
        List<TblBuildingDimBo> list = query.list();
        return list;
    }

    @Override
    public List findTblBuildingDimBySelectWhere(String select, String whereClause) throws Exception {
        StringBuilder buf;
        if (StringUtils.isEmpty(select)) {
            buf = new StringBuilder("from TblBuildingDimBo ");
        } else {
            buf = new StringBuilder(select + " from TblBuildingDimBo b");
        }
        if (!StringUtils.isEmpty(whereClause)) {
            buf.append(whereClause);
        }
        Query query = sessionFactory.getCurrentSession().createQuery(buf.toString());
        @SuppressWarnings("unchecked")
        List list = query.list();
        return list;
    }

    public void deleteTblBuildingDim(String BuildingCode) {

        this.sessionFactory.getCurrentSession().delete(getBuildById(BuildingCode));
//		this.getSession().createQuery("delete from TblBuildingDimBo where Building_code = ?")
//				.setString(0, TblBuildingId).executeUpdate();
    }

    @Override
    public int getBuildingCountByComm(String comm) {
        // TODO Auto-generated method stub
        String hql = "select count(*) from TblBuildingDimBo where 1=1 and comm_code = '" + comm + "'";
        Query query = sessionFactory.getCurrentSession().createQuery(hql);
        return ((Number) query.uniqueResult()).intValue();
    }

    public void deleteTblBuildingDimByComm(String CommCode) {
        try {
            this.deleteByColumns("TblBuildingDimBo", "comm_code", CommCode);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    public TblBuildingDimBo getTblBuilding(TblBuildingDimBo bo) {

        Query q = sessionFactory.getCurrentSession().createQuery("from TblBuildingDimBo where Building_Name = ?");
        q.setParameter(0, new Object[]{bo.getBuildingName()});
        List list = q.list();
        if (list.size() != 0)
            return (TblBuildingDimBo) list.get(0);
        else
            return null;
    }

    public BigInteger getMaxBuildingNum(String commCode) {
        Query q = sessionFactory.getCurrentSession().createSQLQuery("select max(cast(area_name as unsigned)) from tbl_building_dim where comm_code = '" + commCode + "'");
        List list = q.list();
        if (list != null && list.size() != 0)
            return (BigInteger) list.get(0);
        else
            return null;
    }

}	
